package myapp.googlemap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Paint.Join;
import android.util.Log;

public class IconTextDrawable extends android.graphics.drawable.Drawable {

	private String text;
	private final Paint paint;
	Bitmap bmp;
	
	public IconTextDrawable(Context context) {
	    this.text = "";
	    this.paint = new Paint();
	    paint.setColor(Color.RED);
	    int scaledSize = context.getResources().getDimensionPixelSize(R.dimen.myTextSize);
	    paint.setTextSize(scaledSize);
	    paint.setAntiAlias(true);
	    paint.setFakeBoldText(true);
	    paint.setShadowLayer(8f, 0, 0, Color.GRAY);
	    paint.setStyle(Paint.Style.FILL);
	    paint.setTextAlign(Paint.Align.LEFT);
	    bmp = BitmapFactory.decodeResource(context.getResources(),
	            R.drawable.hand_point_icon).copy(Bitmap.Config.ARGB_8888, true);
	    //this.setBounds(-450,-950,250,950);
	}
	
	public IconTextDrawable(Context context, int resource_id , String _text) {
	    this.text = _text;
	    this.paint = new Paint();
	    paint.setColor(Color.GRAY);
	    int scaledSize = context.getResources().getDimensionPixelSize(R.dimen.myTextSize);
	    paint.setTextSize(scaledSize);
	    paint.setAntiAlias(true);
	    //paint.setFakeBoldText(true);
	    paint.setStyle(Paint.Style.FILL_AND_STROKE);
	    
	    //paint.setShadowLayer(0.5f, 0.5f, 0.5f, Color.BLACK);
	    paint.setTextAlign(Paint.Align.CENTER);
	    bmp = BitmapFactory.decodeResource(context.getResources(), resource_id);
	}
	
	public void setText(String text) {
	    this.text = text;
	}
	
	@Override
	public void draw(Canvas canvas) {
	    // TODO Auto-generated method stub
	    int yoff = -1*bmp.getHeight()/2;
	    int xoff = -1*bmp.getWidth()/2;
	    canvas.drawBitmap(bmp, xoff, yoff, paint);
	    
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Join.ROUND);
        paint.setStrokeMiter(10);
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(0.5f);
        
/*        int x = 0, y = bmp.getHeight()/2+10;
        for(String line: text.split("\n")){
        	canvas.drawText(line, x, y, paint);
            y+=paint.ascent()+paint.descent();
        }*/
        
        
        paint.setStyle(Paint.Style.FILL);
        
        paint.setColor(Color.RED);
        int x = 0;
        int y = bmp.getHeight()/2+10;
        for(String line: text.split("\n")){
        	canvas.drawText(line, x, y, paint);
            y+=13f;
        }
	}
	
	@Override
	public int getOpacity() {
	    // TODO Auto-generated method stub
	    return PixelFormat.TRANSLUCENT;
	}
	
	@Override
	public void setAlpha(int alpha) {
	    // TODO Auto-generated method stub
	    paint.setAlpha(alpha);
	}
	
	@Override
	public void setColorFilter(ColorFilter cf) {
	    // TODO Auto-generated method stub
	    paint.setColorFilter(cf);
	}
}